﻿
TableInsList = [];

//位置表示：中央表示
var ALIGN_CENTER = "center";
//位置表示：右側表示
var ALIGN_RIGHT = "right";

Table = function () {

    //定义承载Table的div
    this.divId = "";

    this.tableId = "";

    //json值
    this.data = null;

    //列明及显示字
    this.colList;

    //画Table的单元格的callback
    this.cellCallback;

    //选择checkbox的个数
    this.checkedDataList = [];

    //定义是否显示多选框
    this.useCheckOne = false;

    //定义表的pk列明
    this.pkColumnName = [];

    //选中checkbox时选中rows的pk集合
    this.checkedPKIDList = [];

    this.pager = new Pager();

    this.rowClassCallback = null;

    this.divWidth = 0;

    //获取选中行的数据对象
    this.selectingRowData = {};

};

Table.prototype = {
    //初期化処理
    init: function (divId, json, colList, cellCallback) {

        //赋值json
        if (json.Data != undefined) {
            this.data = json.Data;
        }
        else {
            this.data = json;
        }

        //列名及显示的字体赋值
        this.colList = colList;

        //装载table的div
        this.divId = divId;

        //画table时给单元格提供cellback处理
        this.cellCallback = cellCallback;

        //定义table的id
        this.tableId = "table_" + divId;

        if (this.useCheckOne == true) {
            this.divWidth = parseInt($(this.divId).style.width, 10) - 30;
        }
        else {
            this.divWidth = parseInt($(this.divId).style.width, 10);
        }
        //保存表格数据列表
        if (json.Data != null) {
            this.pager.init(this, json.Pager);
        }
    },
    //创建Table
    create: function () {
        //初期化选中的checkbox的data的个数
        this.checkedDataList = [];
        var strHTML = "";
        //拼接table
        strHTML = "<table class='datatable' id='" + this.tableId + "' cellpadding=0 cellspacing=0 style='width:100%;'>";
        //拼接头部
        strHTML += this.createHeader();
        //拼接body部分
        strHTML += this.createBody();

        strHTML += "</table>";

        //调用分页
        strHTML += this.pager.create();

        //先清空div的text文本，在把table画上去
        $j("#" + this.divId).empty();
        $j("#" + this.divId).append(strHTML);

        //把画好的table放到table集合里面
        TableInsList[this.tableId] = this;

        this.checkedPKIDList = [];
        this.checkedDataList = [];

        this.setRowEvent();
    },
    //table的head调用
    createHeader: function () {

        var tableHeader = new TableHeader();

        tableHeader.init(this, this.colList);

        return tableHeader.create();
    },
    //table的body的实现
    createBody: function () {

        var tableCreate = "";

        if (this.data.length == 0) {
            var tdCount = this.colList.size();

            if (this.useCheckOne) { tdCount = tdCount + 1; }
            return tableCreate = "<tr><td colspan='" + tdCount + "'>没有任何数据!</td></tr>";
        }



        //循环取得json的值
        for (var i = 0; i < this.data.length; i++) {
            var rowCssClass = this.getRowCssClass(this.data[i], i);

            tableCreate += "<tr rowNo=\"" + i + "\" style='font-size:14px;cursor:pointer;' class='" + rowCssClass + "'";
            tableCreate += " >";
            if (this.useCheckOne == true) {
                tableCreate += "<td align=\"center\"> <input type='checkbox' name='" + this.tableId + "_cbItems' onclick='cmnTableCheckOne(\"" + this.tableId + "\", \"" + i + "\")' /></td>";
            }

            //循环取得collist的定义的列名
            for (var j = 0; j < this.colList.size(); j++) {
                var value = "";
                var colInfo = this.colList[j];

                //单元格赋值回调函数
                if (typeof this.cellCallback == "function") {
                    value = this.cellCallback(this.data[i], colInfo.colName, i);
                }
                else {
                    value = this.data[i][colInfo.colName]
                }

                var cellWidth = (this.divWidth * parseFloat(this.colList[j].width) / 100 - 8);

                var text = value.replace(/<[^>].*?>/g, "");

                if (value == text) {
                    value = this.getCellValue(value, cellWidth);
                }
                else {
                    value = value.replace(text, this.getCellValue(text, cellWidth));
                }

                tableCreate += "<td style='text-align:" + colInfo.cellAlign + "' ><div title=\"" + text + "\" class=\"table_cell\" style=\"width:" + cellWidth + "px\">" + value + "</div></td>";
            }
            tableCreate += "</tr>";
        }

        return tableCreate;
    },

    //设置页面的显示条数
    setPageSize: function (count) {
        this.pager.pageSize = count;
    },

    getRowCssClass: function (rowData, rowNo) {
        var rowCssClass = "";

        if (typeof this.rowClassCallback == "function") {
            rowCssClass = this.rowClassCallback(rowData, rowNo);
        }
        return rowCssClass;
    },

    setRowEvent: function () {
        this.setRowMouseMoveEvent();

        this.setRowClickEvent();
    },

    setRowMouseMoveEvent: function () {
        var trSelector = "#" + this.tableId + " tr";

        $j(trSelector).unbind("mouseover");
        $j(trSelector).unbind("mouseout");

        $j(trSelector).mouseover(function () {
            $j(this).addClass("selected");
        });

        $j(trSelector).mouseout(function () {
            $j(this).removeClass("selected");
        });
    },

    setRowClickEvent: function () {
        var tableIns = this;

        var trSelector = "#" + this.tableId + " tr";

        $j(trSelector).unbind("click");

        $j(trSelector).click(function () {
            if ($j(this).hasClass("table_head")) { return; }

            $j(trSelector).removeClass("selecting");
            $j(this).addClass("selecting");

            this.selectingRowData = tableIns.data[$j(this).attr("rowNo")];
            //点击行实现选中和去除选中
            //            if ($j(this).find("input[type=checkbox]").attr("checked") == undefined) {
            //                $j(this).find("input[type=checkbox]").attr("checked", "checked");
            //            }
            //            else {
            //                $j(this).find("input[type=checkbox]").removeAttr("checked");
            //            }
        });
    },

    getCellValue: function (value, width) {
        var strlength = MainScript.getBytesCount(value);

        if (strlength * 6 < width) {
            return value;
        }
        else {
            var index = 0;
            var length = 0
            var resultStr = "";

            while (length < parseInt(width / 6) - 4) {
                resultStr += value.substring(index, index + 1);
                length = MainScript.getBytesCount(resultStr);
                index += 1;
            }

            return resultStr + "...";
        }
    }
};

//实现点击全选时把所有的data都放到checkedDataList中
function cmnTableCheckAll(tableID, checked) {
    //从table集合中找到指定的table
    var tableIns = TableInsList[tableID];

    $j("#" + tableID + " [name=" + tableID + "_cbItems]:checkbox").attr("checked", checked);
    for (var i = 0; i < tableIns.data.length; i++) {
        //先判断是否选中
        if (checked) {
            //判断checkedDataList是否已经存在当前的data
            if (tableIns.checkedDataList.indexOf(tableIns.data[i]) < 0) {
                tableIns.checkedDataList.push(tableIns.data[i]);
                tableIns.checkedPKIDList.push(tableIns.data[i][tableIns.pkColumnName]);
            }
        }
        //移除没有选中的data
        else {
            if (tableIns.checkedDataList.indexOf(tableIns.data[i]) >= 0) {
                var index = tableIns.checkedDataList.indexOf(tableIns.data[i]);
                tableIns.checkedDataList.splice(index, 1);

                index = tableIns.checkedPKIDList.indexOf(tableIns.data[i][tableIns.pkColumnName]);
                tableIns.checkedPKIDList.splice(index, 1);
            }
        }
    }
}
//当点击单个checkbox时
function cmnTableCheckOne(tableID, rowNo) {
    var tableIns = TableInsList[tableID];
    //把选中的data放到checkedDataList中去
    if ($j("#" + tableID + " [name=" + tableID + "_cbItems]").eq(rowNo).attr("checked") == "checked") {
        tableIns.checkedDataList.push(tableIns.data[rowNo]);
        tableIns.checkedPKIDList.push(tableIns.data[rowNo][tableIns.pkColumnName]);
    }
    //移除取消选中的data
    else {
        var index = tableIns.checkedDataList.indexOf(tableIns.data[rowNo]);
        tableIns.checkedDataList.splice(index, 1);

        index = tableIns.checkedPKIDList.indexOf(tableIns.data[rowNo][tableIns.pkColumnName]);
        tableIns.checkedPKIDList.splice(index, 1);
    }

}